package com.mall.product.validation;

import com.mall.product.annotation.NumberValue;

import javax.swing.*;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

public class NumberConstraintValidator implements ConstraintValidator<NumberValue, Integer> {
    private Set<Integer> set = new HashSet<>();
    @Override
    public void initialize(NumberValue constraintAnnotation) {
        int[] numbers = constraintAnnotation.value();
        for (int i : numbers) {
            set.add(i);
        }

    }

    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(integer);
    }
}
